package com.donfu.web.controller.house;

import java.util.List;
import java.util.Map;

import com.donfu.web.controller.tool.CoordinateTool;
import com.donfu.web.controller.tool.MapHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.system.domain.house.HouseStore;
import com.donfu.system.service.house.IHouseStoreService;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;

/**
 * 店铺信息Controller
 * 
 * @author donfu
 * @date 2021-12-01
 */
@Api(tags = "后台店铺信息管理")
@RestController
@RequestMapping("/house/store")
public class HouseStoreController extends BaseController
{
    @Autowired
    private IHouseStoreService houseStoreService;

    /**
     * 查询店铺信息列表
     */
    @ApiOperation("查询店铺信息列表")
    @PreAuthorize("@ss.hasPermi('house:store:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseStore houseStore)
    {
        startPage();
        List<HouseStore> list = houseStoreService.selectHouseStoreList(houseStore);
        return getDataTable(list);
    }

    @ApiOperation("查询店铺信息列表，此类表含有导购人数")
    @PreAuthorize("@ss.hasPermi('house:store:list')")
    @GetMapping("/listHaveEmployeeNumber")
    public TableDataInfo listHaveEmployeeNumber(HouseStore houseStore)
    {
        startPage();
        List<Map<String,Object>> list = houseStoreService.selectHouseStoreListIncluding(houseStore);
        return getDataTable(list);
    }

    /**
     * 导出店铺信息列表
     */
    @ApiOperation("导出店铺信息列表")
    @PreAuthorize("@ss.hasPermi('house:store:export')")
    @Log(title = "店铺信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HouseStore houseStore)
    {
        List<HouseStore> list = houseStoreService.selectHouseStoreList(houseStore);
        ExcelUtil<HouseStore> util = new ExcelUtil<HouseStore>(HouseStore.class);
        return util.exportExcel(list, "店铺信息数据");
    }

    /**
     * 获取店铺信息详细信息
     */
    @ApiOperation("获取店铺详细信息")
    @PreAuthorize("@ss.hasPermi('house:store:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseStoreService.selectHouseStoreById(id));
    }

    /**
     * 新增店铺信息
     */
    @ApiOperation("新增店铺信息")
    @PreAuthorize("@ss.hasPermi('house:store:add')")
    @Log(title = "店铺信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody HouseStore houseStore)
    {
        AjaxResult ajaxResult = checkHouseStore(houseStore);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseStoreService.insertHouseStore(houseStore));
    }

    /**
     * 修改店铺信息
     */
    @ApiOperation("修改店铺信息")
    @PreAuthorize("@ss.hasPermi('house:store:edit')")
    @Log(title = "店铺信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseStore houseStore)
    {
        Long id = houseStore.getId();
        if(id==null){
            return AjaxResult.error("id不能为空");
        }
        AjaxResult ajaxResult = checkHouseStore(houseStore);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseStoreService.updateHouseStore(houseStore));
    }

    /**
     * 删除店铺信息
     */
    @ApiOperation("删除店铺信息")
    @PreAuthorize("@ss.hasPermi('house:store:remove')")
    @Log(title = "店铺信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseStoreService.deleteHouseStoreByIds(ids));
    }

    /**
     * 获取经纬度
     * @param address
     * @return
     */
    @ApiOperation("获取经纬度")
    @GetMapping("/getDu")
    public AjaxResult getLongitudeAndLatitude(String address){
        // 获取经纬度 格式为 “XXXX,YYYY”，前面的是经度，后面的是纬度
        String coordinate = CoordinateTool.getCoordinate(address);
        return AjaxResult.success(coordinate);
    }

    /**
     *  获取两个经纬度之间的距离(前台专属)
     * @param firstParam 地点一的
     * @param secondParam
     * @return
     */
    public double getDistance(String firstParam,String secondParam){
        double distance = MapHelper.GetPointDistance(firstParam, secondParam);
        return distance;
    }

    /**
     * 检查参数是否合格
     * @param houseStore
     * @return
     */
    public AjaxResult checkHouseStore(HouseStore houseStore){
        String name = houseStore.getName();
        if(name!=null && name.trim().length()>66){
            return AjaxResult.error("店铺名称过长");
        }
        String businessDateTime = houseStore.getBusinessDateTime();
        if(businessDateTime!=null && businessDateTime.length()>150){
            return AjaxResult.error("营业时间不必这么详细");
        }
        String address = houseStore.getAddress();
        if(address!=null && address.length()>120){
            return AjaxResult.error("地址字符太长了");
        }
        String standby = houseStore.getStandby();
        if(standby!=null && standby.length()>100){
            return AjaxResult.error("备注太长了");
        }
        return AjaxResult.success();
    }
}
